import connect
import close

'''
加入异常处理，回滚事物
'''
conn = connect.create_connect()
# 创建游标
cursor = conn.cursor()
conn.begin()
try:
    # cursor.executemany("INSERT INTO create_test_01(name,age) VALUES(%s,%s);", [("李生", 20), ('周瑜', None)])
    sql = "INSERT INTO create_test_01(name,age) VALUES(%s,%s);"
    cursor.execute(sql,("李生", 20))
    # 获取新增数据的自增 ID
    new_id = cursor.lastrowid
    print(new_id)
    cursor.execute(sql, ("狗蛋", None))
    conn.commit()
except Exception as e:
    # 回滚
    conn.rollback()
    print(f'执行失败：{e}')
finally:
    close.close(conn, cursor)
